/*
 * BSM6.0
 *
 * Copyright 2012-2013 (C) SINODATA CO., LTD. All Rights Reserved.
 */
package com.sinodata.bsm.center.dao.impl;

import java.util.List;

import org.hibernate.Query;
import org.springframework.stereotype.Repository;

import com.sinodata.bsm.center.dao.BaseDao;
import com.sinodata.bsm.center.dao.ProcessRuleDao;
import com.sinodata.bsm.common.vo.ProcessEventTypeRule;
import com.sinodata.bsm.common.vo.ProcessExcludeResRule;
import com.sinodata.bsm.common.vo.ProcessRule;

/**
 * <p>
 * Description: 
 * </p>
 *
 * @author dongyushi
 * @version 1.0

 * <p>
 * History: 
 *
 * Date                     Author         Version     Description
 * ---------------------------------------------------------------------------------
 * 2013-3-13 上午11:04:56          dongyushi        1.0         To create
 * </p>
 *
 * @since 
 * @see     
 */
@Repository
public class WorkOrderDaoImpl extends BaseDao implements ProcessRuleDao {

    @SuppressWarnings("unchecked")
    @Override
    public List<ProcessRule> findAllByRes(Long resId) {
        String hql = "from ProcessRule t where t.resId=" + resId;
        Query query = this.getSession().createQuery(hql.toString());
        return query.list();
    }

    @Override
    public void batchDeleteWorkOrderEventType(Long[] ids) {
        this.getSession().createQuery("delete from ProcessEventTypeRule where id in (:ids)").setParameterList("ids", ids).executeUpdate();
    }

    @Override
    public int getByNameAndResId(Long resId, String name) {
        StringBuilder hql = new StringBuilder(" select count(*) from ProcessRule where res_id = " + resId + " and name ='" + name + "'");
        Query query = this.getSession().createQuery(hql.toString());
        return ((Long) query.list().iterator().next()).intValue();
    }

    @Override
    public void deleteWorkOrderExcludeRes(Long workOrderId) {
        String hql = "delete from ProcessExcludeResRule where workOrderId=" + workOrderId.longValue();
        getSession().createQuery(hql).executeUpdate();
    }

    @Override
    public void deleteWorkOrderExcludeResById(Long id) {
        String hql = "delete from ProcessExcludeResRule where id=" + id.longValue();
        getSession().createQuery(hql).executeUpdate();
    }

    @Override
    public void deleteWorkOrderEventType(Long workOrderId) {
        String hql = "delete from ProcessEventTypeRule where workOrderId=" + workOrderId.longValue();
        getSession().createQuery(hql).executeUpdate();
    }

    @SuppressWarnings("unchecked")
    @Override
    public List<ProcessExcludeResRule> getWorkOrderExcludeResByWorkOrderId(Long workOrderId) {
        String hql = "from ProcessExcludeResRule t where t.workOrderId=" + workOrderId;
        Query query = this.getSession().createQuery(hql.toString());
        return query.list();
    }

    @SuppressWarnings("unchecked")
    @Override
    public List<ProcessEventTypeRule> getWorkOrderEventTypeByWorkOrderId(Long workOrderId) {
        String hql = "from ProcessEventTypeRule t where t.workOrderId=" + workOrderId;
        Query query = this.getSession().createQuery(hql.toString());
        return query.list();
    }

}
